import 'package:fig/sqlite/table_master_config.dart';
import 'package:fig/sqlite/table_master_media_spider.dart';
import 'package:fig/sqlite/table_media.dart';
import 'package:fig/sqlite/table_media_actor.dart';
import 'package:fig/sqlite/table_media_area.dart';
import 'package:fig/sqlite/table_media_category.dart';
import 'package:fig/sqlite/table_media_lang.dart';
import 'package:fig/sqlite/table_media_tag.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class SqlManager {
  static const _MASTER_VERSION = 1;
  static const _REAL_VERSION = 1;

  static const _MASTER_DB = 'config.db';
  static Database _configDb;

  // 真正的数据库
  static Database _realDb;

  static init() async {
    String baseDbPath = await getDatabasesPath();
    String dbPath = join(baseDbPath, _MASTER_DB);
    // await deleteDatabase(dbPath); // 这里是为了测试后面的创建流程
    print('准备打开配置数据库:$dbPath');
    _configDb = await openDatabase(dbPath, version: _MASTER_VERSION, onCreate: masterDbCreate, onUpgrade: masterDbCreate);
    print('已经打开配置数据库:$dbPath');

    await TableConfig().initConfig(_configDb);
    String realDbName = await TableConfig().realDbName();

    dbPath = join(baseDbPath, realDbName) + '.db';
    // await deleteDatabase(dbPath); // 这里是为了测试后面的创建流程
    print('准备打开存储数据库:$dbPath');
    _realDb = await openDatabase(dbPath, version: _REAL_VERSION, onCreate: realDbCreate, onUpgrade: realDbCreate);
  }

  static realDbCreate(Database db, int oldVersion, [int newVersion]) async {
    // 创建的时候，newVersion为空
    if (newVersion == null || newVersion == 0) {
      newVersion = oldVersion;
    }
    print('oldVersion:$oldVersion  newVersion:$newVersion');

    switch (newVersion) {
      case 1:
        realCreateVersion1(db);
        break;
    }
  }

  static masterDbCreate(Database db, int oldVersion, [int newVersion]) async {
    // 创建的时候，newVersion为空
    if (newVersion == null || newVersion == 0) {
      newVersion = oldVersion;
    }
    print('oldVersion:$oldVersion  newVersion:$newVersion');

    switch (newVersion) {
      case 1:
        masterCreateVersion1(db);
        break;
    }
  }

  // 获取当前数据库对象
  static Future<Database> getConfigDatabase() async {
    if (_configDb == null) {
      await init();
    }

    return _configDb;
  }

  // 获取当前数据库对象
  static Future<Database> getCurrentDatabase() async {
    if (_realDb == null) {
      await init();
    }

    return _realDb;
  }

  // 初始化
  static masterCreateVersion1(Database db) async {
    print('masterCreateVersion1:开始初始化');
    TableMediaPool().init(db);
    TableConfig().init(db);
    print('masterCreateVersion1:创建 完成');
  }

  // 初始化
  static realCreateVersion1(Database db) async {
    print('realCreateVersion1:开始初始化');
    TableMedia().init(db);
    TableMediaActor().init(db);
    TableMediaActorMap().init(db);
    TableMediaArea().init(db);
    TableMediaAreaMap().init(db);
    TableMediaCategory().init(db);
    TableMediaCategoryMap().init(db);
    TableMediaLang().init(db);
    TableMediaLangMap().init(db);
    TableMediaTag().init(db);
    TableMediaTagMap().init(db);
    print('realCreateVersion1:创建 完成');
  }
}

abstract class TableBase {
  static final String columnId = "id";
  static final String columnCreateTime = "create_time";

  init(Database db) {
    createTable(db);
    insertDefault(db);
  }

  createTable(Database db);

  insertDefault(Database db) {}
}
